package com.qqbot.impl.task;

import com.qqbot.core.core.Bot;
import com.qqbot.core.core.BotFactory;
import com.qqbot.core.message.MessageChain;
import com.qqbot.impl.service.SinaWeiBoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.List;

/**
 * 整点报时
 *
 * @author changwenpeng
 * @date 2022年5月11日
 */
@Slf4j
@Component
public class RobotTaskBaoShi {

    @Value("${mybot.baoShiGroups}")
    Long[] baoShiGroups;

    @Autowired
    SinaWeiBoService sinaWeiBoService;

    /**
     * 每天7-22点整点报时
     */
    @Scheduled(cron = "0 0 6-22 * * ?")
    public void baoShi() throws Exception {
        String hourStr = "";
        Calendar calendar = Calendar.getInstance();
        // 24小时制
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = hour + "";
        }
        String url = "https://ovooa.com/API/BaoShi/mp3/" + hourStr + ".mp3";
        this.sendbaoShi(new MessageChain().record(url));
    }

    /**
     * 发送消息
     *
     * @param mc
     * @throws Exception
     */
    public void sendbaoShi(MessageChain mc) {
        List<Bot> botList = BotFactory.getBots();
        if (CollectionUtils.isEmpty(botList)) {
            return;
        }
        Bot bot = botList.get(0);
        for (Long groupId : baoShiGroups) {
            System.out.println(mc.toMessageString());
            bot.sendGroupMessage(groupId, mc);
        }
    }

}
